package com.nail.zhaogr.action;

import java.util.Enumeration;
import java.util.List;

import javacommon.base.BaseStruts2Action;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import cn.org.rapid_framework.page.Page;

import com.nail.zhaogr.model.PublishInfo;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

/**
 * 
 * @Class Name: BaseAction.java
 * @Content Description: 
 * @author:nail.zhang
 */
public abstract class BaseAction extends BaseStruts2Action implements Preparable,ModelDriven  {	

	private String url;
	
	 public  void init()  throws Exception
     {
		 getPageUrl();
		 HttpSession session = ServletActionContext.getRequest().getSession(); 
		 if(session.getAttribute("userInfo")==null)
		 {
			 this.addActionMessage("请登陆！！");
			 ServletActionContext.getResponse().sendRedirect( ServletActionContext.getRequest().getContextPath()+"/asdf.jsp");
			 return;
		 }

     }
	 
	 protected void getPageUrl() {
			String requestUrl = ServletActionContext.getRequest().getRequestURL().toString();
			Enumeration list = ServletActionContext.getRequest().getParameterNames();
			int i=0;
			while(list.hasMoreElements()){
				String key = list.nextElement()+"";
				if(!key.equals("pageNumber")&&!key.equals("pageSize")){
					if(i==0){
						requestUrl +="?"+key+"="+ServletActionContext.getRequest().getParameter(key);
					}else{
						requestUrl +="&"+key+"="+ServletActionContext.getRequest().getParameter(key);
					}
					i++;
				}
			}
			if(i==0){
				requestUrl +="?temp=1";
			}
			this.url = requestUrl;
		}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	
	protected void limitLength(Page page){
		List list = page.getResult();
		for (int i = 0; i < list.size(); i++) {
			PublishInfo publishInfo = (PublishInfo)list.get(i);
			String profile = publishInfo.getProfile();
			if(profile.length()>50){
				profile = profile.substring(0,50) +"...";
			}
			publishInfo.setProfile(profile);
		}
	}
	
	protected void limitLength(List list){
		for (int i = 0; i < list.size(); i++) {
			PublishInfo publishInfo = (PublishInfo)list.get(i);
			String profile = publishInfo.getProfile();
			if(profile.length()>50){
				profile = profile.substring(0,50) +"...";
			}
			publishInfo.setProfile(profile);
		}
	}
}
